package io.gitee.chearnee.fw.common.component;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.time.LocalDate;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/6/23 11:32
 */
public class CustomLocalDateDeserializer extends StdDeserializer<LocalDate> {

    public CustomLocalDateDeserializer() {
        this(null);
    }

    protected CustomLocalDateDeserializer(Class<LocalDate> vc) {
        super(vc);
    }

    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException {
        String longValue = p.getValueAsString();
        if (!StringUtils.hasText(longValue)) {
            return null;
        }
        return LocalDateTimeUtil.of(Long.parseLong(longValue)).toLocalDate();
    }
}